﻿/*
Copyright (c) 2011 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System.Collections.Generic;
using System.Reflection;
using HaterAide.Mappings;
using HaterAide.Reflection;
using HaterAide.Settings;
using HaterAide.SQL;
#endregion

namespace HaterAide
{
    /// <summary>
    /// Class that creates all necessary classes
    /// </summary>
    public class Factory
    {
        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="BusinessObjectsAssembly">Assembly containing business objects</param>
        /// <param name="ConnectionString">Connection string</param>
        public Factory(Assembly BusinessObjectsAssembly, string ConnectionString)
        {

            Setup(BusinessObjectsAssembly,ConnectionString);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="BusinessObjectsAssembly">Assemblies containing business objects</param>
        /// <param name="ConnectionString">Connection string</param>
        public Factory(List<Assembly> BusinessObjectsAssembly, string ConnectionString)
        {
            Setup(BusinessObjectsAssembly,ConnectionString);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="BusinessObjectsAssemblyDirectory">Directory containing assemblies
        /// containing business objects</param>
        /// <param name="ConnectionString">Connection string</param>
        public Factory(string BusinessObjectsAssemblyDirectory, string ConnectionString)
        {
            List<Assembly> Assemblies = Utilities.Reflection.Reflection.GetAssembliesFromDirectory(BusinessObjectsAssemblyDirectory);
            Setup(Assemblies, ConnectionString);
        }

        #endregion

        #region Public Static Functions

        /// <summary>
        /// Creates a session object
        /// </summary>
        /// <returns>A newly created session object</returns>
        public static Session CreateSession()
        {
            return new Session(ProviderManager.Providers, CacheProviderManager.CurrentProvider);
        }

        #endregion

        #region Private Functions

        /// <summary>
        /// Sets up the factory
        /// </summary>
        /// <param name="Assembly">Assembly to search within</param>
        /// <param name="ConnectionString">Connection string</param>
        private void Setup(Assembly Assembly,string ConnectionString)
        {
            SetupConfigs();
            MappingManager = new MappingManager(Assembly);
            Gestalt.Manager.Instance.GetConfigFile<Settings.AspectSettings>("HaterAideAspect").ClassMappings = MappingManager.ClassMappings;
            DotExtension.Manager.Instance.Setup(typeof(HaterAide.Factory).Assembly);
            Settings = Gestalt.Manager.Instance.GetConfigFile<Settings.Settings>("HaterAide");
            ClassManager = new ClassManager(MappingManager);
            ProviderManager = new ProviderManager(MappingManager,ClassManager, Settings, ConnectionString);
            CacheProviderManager = new HaterAide.Cache.ProviderManager(Settings);
            CacheProviderManager.SetSession();
        }

        /// <summary>
        /// Sets up the factory
        /// </summary>
        /// <param name="Assemblies">Assemblies to search within</param>
        /// <param name="ConnectionString">Connection string</param>
        private void Setup(List<Assembly> Assemblies,string ConnectionString)
        {
            SetupConfigs();
            MappingManager = new MappingManager(Assemblies);
            Gestalt.Manager.Instance.GetConfigFile<Settings.AspectSettings>("HaterAideAspect").ClassMappings = MappingManager.ClassMappings;
            DotExtension.Manager.Instance.Setup(typeof(HaterAide.Factory).Assembly);
            Settings = Gestalt.Manager.Instance.GetConfigFile<Settings.Settings>("HaterAide");
            ClassManager = new ClassManager(MappingManager);
            ProviderManager = new ProviderManager(MappingManager, ClassManager, Settings, ConnectionString);
            CacheProviderManager = new HaterAide.Cache.ProviderManager(Settings);
            CacheProviderManager.SetSession();
        }

        /// <summary>
        /// Sets up config objects if they are not found
        /// </summary>
        private void SetupConfigs()
        {
            try
            {
                Gestalt.Manager.Instance.GetConfigFile<Gestalt.Interfaces.IConfig>("AspectusConfiguration");
            }
            catch
            {
                Gestalt.Manager.Instance.RegisterConfigFile("AspectusConfiguration", new AspectusSettings());
            }

            try
            {
                Gestalt.Manager.Instance.GetConfigFile<Gestalt.Interfaces.IConfig>("DotCache");
            }
            catch
            {
                Gestalt.Manager.Instance.RegisterConfigFile("DotCache", new CacheSettings());
            }
            try
            {
                Gestalt.Manager.Instance.GetConfigFile<Settings.Settings>("HaterAide");
            }
            catch
            {
                Gestalt.Manager.Instance.RegisterConfigFile("HaterAide", new Settings.Settings());
            }
            try
            {
                Gestalt.Manager.Instance.GetConfigFile<Gestalt.Interfaces.IConfig>("LogFile");
            }
            catch
            {
                Gestalt.Manager.Instance.RegisterConfigFile("LogFile", new LoggingSettings());
            }
            try
            {
                Gestalt.Manager.Instance.GetConfigFile<Settings.AspectSettings>("HaterAideAspect");
            }
            catch
            {
                Gestalt.Manager.Instance.RegisterConfigFile("HaterAideAspect", new Settings.AspectSettings());
            }
        }

        #endregion

        #region Private Static Variables

        private static Settings.Settings Settings { get; set; }
        private static ClassManager ClassManager { get; set; }
        private static SQL.ProviderManager ProviderManager { get; set; }
        private static Cache.ProviderManager CacheProviderManager { get; set; }
        private static MappingManager MappingManager { get; set; }

        #endregion
    }
}